package com.dragon.modules.yuegou.client.wechat_mini.service.impl;

import com.dragon.modules.yuegou.client.wechat_mini.client.WxaCodeClient;
import com.dragon.modules.yuegou.client.wechat_mini.model.AccessToken;
import com.dragon.modules.yuegou.client.wechat_mini.request.QrcodeRequest;
import com.dragon.modules.yuegou.client.wechat_mini.request.UnlimitedQrcodeRequest;
import com.dragon.modules.yuegou.client.wechat_mini.security.AccessTokenService;
import com.dragon.modules.yuegou.client.wechat_mini.service.WxaCodeService;
import com.dragon.modules.yuegou.web.Check;

/**
 * 微信小程序码服务实现
 * 
 * @author liuwu
 *
 */
public class WxaCodeServiceImpl implements WxaCodeService {

	/**
	 * 定义消息客户端
	 */
	private final WxaCodeClient client;

	/**
	 * 凭证服务
	 */
	private final AccessTokenService tokenService;

	/**
	 * 构造一个订阅消息服务
	 * 
	 * @param client       订阅消息客户端
	 * @param tokenService 凭证服务
	 */
	public WxaCodeServiceImpl(WxaCodeClient client, AccessTokenService tokenService) {
		super();
		Check.notNull(client, "client is required");
		Check.notNull(tokenService, "tokenService is required");
		this.client = client;
		this.tokenService = tokenService;
	}

	@Override
	public byte[] createQRCode(String path, Integer width) {
		AccessToken accessToken = tokenService.getAccessToken();
		String token = accessToken.getValue();
		return client.createQRCode(token, path, width);
	}

	@Override
	public byte[] get(QrcodeRequest request) {
		AccessToken accessToken = tokenService.getAccessToken();
		String token = accessToken.getValue();
		return client.get(token, request);
	}

	@Override
	public byte[] getUnlimited(UnlimitedQrcodeRequest request) {
		AccessToken accessToken = tokenService.getAccessToken();
		String token = accessToken.getValue();
		return client.getUnlimited(token, request);
	}

}
